package com.lcb.consumer.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestController {
    public static final String SERVICE_URL = "http://provider";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/test/{id}")
    //@SentinelResource(value = "fallback")
    @SentinelResource(value = "fallback", fallback = "handlerFallback")
    public String test(@PathVariable Integer id) {
        String forObject = restTemplate.getForObject(SERVICE_URL + "/test", String.class);
        if (id == 2)
            throw new IllegalArgumentException("参数异常");
        if (id == 3)
            throw new NullPointerException("空指针异常");
        return forObject;
    }

    public String handlerFallback(@PathVariable Long id, Exception e) {
        System.out.println("进入回调异常");
        return "444   异常" + id + e.getMessage();
    }
}
